package org.springblade.common.config;


import org.springblade.common.constant.R;
import org.springblade.common.exception.MyServiceException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wukq
 * @create 2024/4/9 17:34
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(MyServiceException.class)
	public ResponseEntity<R> handleMyServiceException(MyServiceException e) {
		R r = new R();
		r.setSuccess(false);
		r.setCode(e.getResultCode().getCode());
		r.setMsg(e.getMessage());
		HttpStatus httpStatus = determineStatus(e.getResultCode().getHttpCode());
		return new ResponseEntity<>(r, httpStatus);
	}

	public HttpStatus determineStatus(int code) {
		return HttpStatus.valueOf(code);
	}
}
